package com.hushao.lims.weblims.service.impl;

import com.hushao.lims.weblims.domain.*;
import com.hushao.lims.weblims.repository.ClassRepository;
import com.hushao.lims.weblims.repository.ClassroomRepository;
import com.hushao.lims.weblims.repository.TermRepository;
import com.hushao.lims.weblims.repository.TimetableRepository;
import com.hushao.lims.weblims.service.TimetableService;
import com.hushao.lims.weblims.util.DateUtil;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;

/**
 * @author 930588706
 * @date 18/2/3
 */
@Service
public class TimetableServiceImpl implements TimetableService {
    @Autowired
    private TimetableRepository timetableRepository;
    @Autowired
    private TermRepository termRepository;
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private ClassRepository classRepository;
    @Autowired
    private ClassroomRepository classroomRepository;
    @Override
    public Page<TimetableInfo> listClassByTeacherNameAndTerm(String teacherName, String year, String term, Pageable pageable) {
        teacherName="%"+teacherName+"%";
        String newTerm;
        switch (term){
            case "0":
                if("".equals(year)){
                    newTerm="01";
                }else {
                newTerm=year+(Integer.parseInt(year)+1)+"01";}
                break;
            case "1":
                if ("".equals(year)){
                    newTerm="07";
                }else {
                newTerm=year+year+"07";}
                break;
            default: newTerm=year;
        }
        newTerm="%"+newTerm+"%";
        return timetableRepository.findByTeacherInfo_TeacherNameLikeAndTermInfo_TermLike(teacherName,newTerm,pageable);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void uploadFile(Sheet sheet, String termStartTime) throws Exception {
        String term;
        int change=DateUtil.getWeek(Integer.parseInt(termStartTime.substring(0,4)),Integer.parseInt(termStartTime.substring(5,7)),Integer.parseInt(termStartTime.substring(8,10)));
        if(change!=1){
            throw new Exception("开学日期必须为星期一");
        }
        if(Integer.parseInt(termStartTime.substring(5,7))>9){
            term=termStartTime.substring(0,4)+(Integer.parseInt(termStartTime.substring(0,4))+1)+"01";
        }else { term=termStartTime.substring(0,4)+termStartTime.substring(0,4)+"07";

        }
        TermInfo termInfo=termRepository.findByTerm(term);
        if (termInfo == null){
            TermInfo t=new TermInfo(termStartTime.replace("-",""),term,null);
            termRepository.save(t);
        }else {
            termInfo.setTermStartTime(termStartTime.replace("-",""));
            termRepository.flush();
        }




        List<TimetableInfo> timetables=new LinkedList<>();

        for (int i=1; i<=sheet.getLastRowNum(); i++) {
            //获取索引为i的行，以0开始
            Row row = sheet.getRow(i);
            row.getCell(0).setCellType(CellType.STRING);
            String number = row.getCell(0).getStringCellValue();
            row.getCell(1).setCellType(CellType.STRING);
            String className = row.getCell(1).getStringCellValue();
            row.getCell(2).setCellType(CellType.STRING);
            String classroomName = row.getCell(2).getStringCellValue();
            row.getCell(3).setCellType(CellType.STRING);
            String classTime = row.getCell(3).getStringCellValue();
            TeacherInfo teacher= (TeacherInfo) userDetailsService.loadUserByUsername(number);
            ClassInfo classInfo=classRepository.findByClassName(className);
            if (classInfo==null){
                throw new Exception("第"+i+"行班级错误！");
            }
            ClassroomInfo classroom=classroomRepository.findByClassroomName(classroomName);
            if (classroom==null){
                throw new Exception("第"+i+"行教室错误！");
            }
            timetables.add(new TimetableInfo(teacher,classInfo,classroom,classTime,termInfo));
        }
        timetableRepository.saveAll(timetables);
    }

}
